package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.ManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("management")
@Slf4j
public class ManagementController {
    @Reference
    private ManagementService managementService;
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("edit")
    public Result edit(@RequestBody Permission permissions){
        log.debug("permissions:{}",permissions);
        try {
            //调用service 方法
            managementService.edit(permissions);
            //返回成功消息
            return new Result(true,"编辑权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败消息
            return new Result(false,"编辑权限失败");
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            //调用service 方法
            Permission permissions = managementService.findById(id);
            //返回成功数据
            return new Result(true,"查询成功",permissions);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败信息
            return new Result(false,"查询失败");
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("delete")
    public Result deleteById(Integer id){
        try {
            //调用service 方法
            managementService.deleteById(id);
            //删除成功,返回消息
            return new Result(true,"删除权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败,返回消息
            return new Result(false,"删除权限失败");
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("add")
    public Result add(@RequestBody Permission permission){
        try {
            //调用service 实现新增
            managementService.add(permission);
            return new Result(true, "新增权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增权限失败");
        }
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            //调用service 获取分页结果
            PageResult pageResult = managementService.pageQuery(queryPageBean);
            //成功,返回结果
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            //失败,返回初始化结果
            return new PageResult(0l,null);
        }
    }
}
